package com.mobileares.midp.widgets.client.pop;

import com.google.gwt.dom.client.EventTarget;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.*;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-7-15
 * Time: 15:13:58
 * To change this template use File | Settings | File Templates.
 */
public class GwtPop extends PopupPanel {

    private boolean dragging;
    private int dragStartX, dragStartY;

    private PopWidget pop;

    public GwtPop() {
        pop = new PopWidget();
        pop.setCloseHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                hide();
            }
        });
        HorizontalPanel wrapper = new HorizontalPanel();
        pop.setWidth("96%");
        wrapper. add(pop);
        super.setWidget(wrapper);
        setGlassEnabled(true);
        setAnimationEnabled(true);
        setAutoHideEnabled(false);
        addMouserEvent(pop.main);

    }

    public void setWidget(Widget widget) {
        pop.body.add(widget);
    }
    public void setHeadText(String text){
        pop.setHeadText(text);
    }


    @Override
    protected void onPreviewNativeEvent(Event.NativePreviewEvent event) {
        // We need to preventDefault() on mouseDown events (outside of the
        // DialogBox content) to keep text from being selected when it
        // is dragged.
        NativeEvent nativeEvent = event.getNativeEvent();

        if (!event.isCanceled()
                && (event.getTypeInt() == Event.ONMOUSEDOWN)
                && isCaptionEvent(nativeEvent)) {
            nativeEvent.preventDefault();
        }

        super.onPreviewNativeEvent(event);
    }

    private boolean isCaptionEvent(NativeEvent event) {
        EventTarget target = event.getEventTarget();
        if (Element.is(target)) {
            return pop.main.getElement().isOrHasChild(
                    Element.as(target));
        }
        return false;
    }

    public void addMouserEvent(FocusPanel caption) {
        caption.addMouseDownHandler(new MouseDownHandler() {
            public void onMouseDown(MouseDownEvent event) {
                dragging = true;
                DOM.setCapture(pop.main.getElement());
                dragStartX = event.getX();
                dragStartY = event.getY();
            }
        });
        caption.addMouseMoveHandler(new MouseMoveHandler() {
            public void onMouseMove(MouseMoveEvent event) {
                if (dragging) {
                    addStyleName("glass-move");
                    int absX = event.getX() + getAbsoluteLeft();
                    int absY = event.getY() + getAbsoluteTop();
                    setPopupPosition(absX - dragStartX, absY - dragStartY);
                }
            }
        });
        caption.addMouseUpHandler(new MouseUpHandler() {
            public void onMouseUp(MouseUpEvent event) {
                removeStyleName("glass-move");
                dragging = false;
                DOM.releaseCapture(pop.main.getElement());
            }
        });
    }


}
